local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"

require "hbslenum"

local server = nil
local noderequest = {}

function noderequest.init(service)
	if server == nil then server = service end
end


--[[
message levelitem {
	optional int32 level_type 		= 1;	//场次类型 1初级 2中级 3高级
	optional int32 min_hb_amount 	= 2;	//最小红包金额
	optional int32 max_hb_amount 	= 3;	//最大红包金额
	optional int32 cur_total_player = 4;	//当前总人数
	optional int32 hb_num 			= 5;		// 红包个数
	optional int32	times			= 6; 		//倍数
}
//获得场次列表
message getlevellist {
}

message getlevellistR {
	repeated levelitem levellist = 1; 
}
--]]
function noderequest.getlevellist(rid, roledata, msgbody)
	local responsemsg = {
		levellist = {},
	}
	responsemsg.levellist = server.CMD["get_levellist"]()
	return responsemsg
end
--[[
//进桌
message entertable {
	optional int32 level_type = 1; 
}

message entertableR {
	optional table_gameinfo table_gameinfo = 1;	//返回桌子的信息
}
--]]
function noderequest.entertable(rid, roledata, msgbody)
	local responsemsg = {}
	if server:get_server_flag() then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "服务器维护中"
		return responsemsg
	end

	local status,is_error = skynet.pcall(cluster.call,"gatenode",".router","check_role_state",rid, 0)
    if not status then
        responsemsg.retcode = EErrCode.ERR_INVALID_TABLE_INDEX
		responsemsg.retstr = "在其他桌内"
        return responsemsg
	end
	if is_error == true then
		responsemsg.retcode = EErrCode.ERR_INVALID_TABLE_INDEX
		responsemsg.retstr = "在其他桌内"
        return responsemsg
	end
	
	local idle_table_index = server.CMD["get_idle_table_index"](msgbody.level_type)

	if not server.index_to_table[idle_table_index] then
		responsemsg.retcode = EErrCode.ERR_INVALID_TABLE_INDEX
		responsemsg.retstr = "没有找到桌子！"
	else
		local table_servicemap = server.table_service_map[server.index_to_table[idle_table_index].create_table_id]
		if not table_servicemap or not table_servicemap.conf then
			responsemsg.retcode = EErrCode.ERR_INVALID_TABLE_INDEX
			responsemsg.retstr = "没有找到桌子！"
			return responsemsg
		end
		if table_servicemap.conf.is_closed == true then
			responsemsg.retcode = EErrCode.ERR_INVALID_TABLE_INDEX
			responsemsg.retstr = "敬请期待"
			return responsemsg
		end
		local table_service = server.index_to_table[idle_table_index].table_service
		local id = server.index_to_table[idle_table_index].id

		local ok, result = pcall(skynet.call, table_service, "lua", "entertable", rid, roledata, msgbody)
		responsemsg = result
		if responsemsg.retcode == nil then
			server:forward(rid, table_service, id)
		end
	end
	return responsemsg
end

function noderequest.disconnect(rid, roledata, msgbody)
	local responsemsg = {}
	server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
	return responsemsg
end

function noderequest.reentertable(rid, roledata, msgbody)
	local responsemsg = {}
	server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
	return responsemsg
end

function noderequest.getpubtablelist(rid, roledata, msgbody)
	local list = {
		mylist = {},
		publist = {},
	}
	--获得匹配场信息
	list.matchlist = server.CMD["get_levellists"]()
	return list
end

function noderequest.leavetable(rid, roledata, msgbody)
	local responsemsg = {}
	server:notice_tableinfo_togatenode("role_leave_table", rid, skynet.getenv("nodename"), 0)
	return responsemsg
end

function noderequest.updata_weight_lists(new_w_list)
	skynet.send(".slotmgr", "lua", "update_picture_weight_list", new_w_list)
end

function noderequest.set_system_lose_limit(loselimit_base, loselimit_factor)
	skynet.send(".slotmgr", "lua", "set_system_lose_limit", loselimit_base, loselimit_factor)
end

function noderequest.set_game_robot_num(base_num, inc_factor, min_num)
    skynet.send(".robotmgr", "lua", "set_game_robot_num", base_num, inc_factor, min_num)
end

function noderequest.set_game_robot_state(is_run)
	skynet.send(".robotmgr", "lua", "set_game_robot_state", is_run)
end

function noderequest.get_game_robot_state()
	return skynet.call(".robotmgr", "lua", "get_game_robot_state")
end

function noderequest.get_game_robot_num()
	return skynet.call(".robotmgr", "lua", "get_game_robot_num")
end

function noderequest.notice_alltable_robot_open()
	for create_table_id, table_item in pairs(server.table_service_map) do
		skynet.send(table_item.table_service, "lua", "notice_table_robotopen")
	end
end

return noderequest